//
//  Segment.m
//  NewsMaker Touch
//
//  Created by Reisen on 9/23/14.
//  Copyright (c) 2014 Reisen. All rights reserved.
//

#import "Segment.h"
#import "Utility.h"
//#import "EffectsManager.h"

@implementation Segment

-(NSDate*)lastModified
{
    return [[[NSFileManager defaultManager] attributesOfItemAtPath:[self fullPath] error:nil] fileModificationDate];
}

-(void)setLastModified:(NSDate *)modifyDate
{
    [[NSFileManager defaultManager] setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:modifyDate, NSFileModificationDate, nil] ofItemAtPath:[self fullPath] error:nil];
}

-(id)initWithDictionary:(NSDictionary*)dict
{
    self = [super init];
    
    self.name = [dict objectForKey:@"name"];
    self.type = [[dict objectForKey:@"type"] integerValue];
    self.actor = [dict objectForKey:@"actor"];
    self.note = [dict objectForKey:@"note"];
    self.storageFolder = [dict objectForKey:@"storageFolder"];
    
    return self;
}

-(NSMutableDictionary*)dictionaryRepresentation
{
    NSMutableDictionary* dict = [NSMutableDictionary dictionary];
    
    [dict setObject:self.name forKey:@"name"];
    [dict setObject:[NSNumber numberWithInteger:self.type] forKey:@"type"];
    
    if(self.actor != nil)
        [dict setObject:self.actor forKey:@"actor"];

    [dict setObject:self.note forKey:@"note"];
    [dict setObject:self.storageFolder forKey:@"storageFolder"];
    
    return dict;
}

-(NSString*)fullPath
{
    return [self.root stringByAppendingPathComponent:self.storageFolder];
}

-(void)save
{
    
}

-(void)load
{
    
}

-(void)initializePreview
{
    
}
-(void)play
{
    
}
-(void)pause
{
    
}
-(void)stop
{
    
}


-(BOOL)canPreview
{
    return NO;
}

-(UIColor*)colorCode
{
    
    if(self.type == SegmentTypeTextInsert)
        return [UIColor colorWithRed:248/255.0 green:0 blue:106/255.0 alpha:1];
    
    else if(self.type == SegmentTypeTrailer)
        return [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
    
    else if(self.type == SegmentTypeWebcam)
        return [UIColor colorWithRed:45/255.0 green:190/255.0 blue:10/255.0 alpha:1];
    
    else if(self.type == SegmentTypeVideoInsert)
        return [UIColor colorWithRed:60/255.0 green:120/255.0 blue:252/255.0 alpha:1];
    else if(self.type == SegmentTypeVoiceImage)
        return [UIColor colorWithRed:246/255.0 green:118/255.0 blue:29/255.0 alpha:1];
    else if(self.type == SegmentTypeVoiceVideo)
        return [UIColor colorWithRed:144/255.0 green:0/255.0 blue:252/255.0 alpha:1];
    
    return [UIColor whiteColor];
}

-(void) encodeVideo
{
    
}


-(CGFloat)seekToTime:(float)gap
{
    return gap;
}

-(void) initializePreviewInView:(UIView*)layoutView
{
    
}


-(void)recordingFinished:(NSURL*)outputURL
{
    [self.encodingDelegate performSelector:self.encodingCompleted withObject:self];
}


-(NSString*)videoInformation
{
    return [[self fullPath] stringByAppendingPathComponent:@"output.mp4"];
}
-(NSMutableArray*)audioInformationWithStart:(float)start
{
    return nil;
}

-(BOOL)isValid
{
    return self.actor != nil;
}

-(void)loadBackgroundInView:(UIView*)view fromDict:(NSDictionary*)dict
{
    //*
    UIImageView* backgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    [backgroundView setContentMode:UIViewContentModeScaleAspectFill];
    
    
    backgroundView.backgroundColor = [Utility UIColorFromNSString:dict[@"backgroundColor"]];
    
    NSString* selectedBackgroundImage = dict[@"backgroundImage"];
    
    if(selectedBackgroundImage == nil) backgroundView.image = nil;
    
    else
    {
//        NSString* bgFolderPath =
        //[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Backgrounds"];
        
        NSString* path = [self.fileRoot stringByAppendingPathComponent:selectedBackgroundImage];
        
        if(![[NSFileManager defaultManager] fileExistsAtPath:path])
            path = [[self fullPath] stringByAppendingPathComponent:selectedBackgroundImage];
        
        backgroundView.image = [Utility resizedImage:[UIImage imageWithContentsOfFile:path] size:backgroundView.bounds.size];
    }
    
    backgroundView.transform = CGAffineTransformIdentity;
    
    if(dict[@"backgroundTransform"] != nil)
    {
        backgroundView.transform = CGAffineTransformFromString(dict[@"backgroundTransform"]);
        
        if(backgroundView.transform.a == 0)
        {
            CGRect myFrame = backgroundView.frame;
            backgroundView.frame = CGRectMake(CGRectGetMidX(myFrame) - CGRectGetHeight(myFrame) / 2, CGRectGetMidY(myFrame) - CGRectGetWidth(myFrame) / 2, CGRectGetHeight(myFrame), CGRectGetWidth(myFrame));
        }
    }
    
    [view addSubview:backgroundView];
     //*/
}
@end
